/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("View_OrderInfo",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.Demo.IServices;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using Dapper;
using System.Linq;
using VOL.Core.Utilities;
using Sys = System.IO;
using OfficeOpenXml;
using System.Net.Mime;
using System.Drawing;
using VOL.Core.Infrastructure;

namespace VOL.Demo.Controllers
{
    public partial class View_OrderInfoController
    {
        private readonly IView_OrderInfoService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public View_OrderInfoController(
            IView_OrderInfoService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 自定义导出
        /// </summary>
        /// <param name="loadData"></param>
        /// <returns></returns>
        [HttpPost, Route("customExport")]
        public IActionResult CustomExport([FromBody] List<SearchParameters> wheres)
        {

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            rows.Add(new Dictionary<string, object>() { { "测试1", "123" }, { "测试2", "456" } });
            rows.Add(new Dictionary<string, object>() { { "测试1", "789" }, { "测试2", "1012" } });

            //调用通过导出
            string path = EPPlusHelper.ExportGeneralExcel(rows, "text.xlsx", onFillCell: (ExcelWorksheet worksheet, int rowIndex, int columnIndex, object value) =>
            {
                //自定义excel内容或者格式，见epplus文档
            });
            return File(Sys.File.ReadAllBytes(path), MediaTypeNames.Application.Octet, "text.xlsx");

            //获取查询条件
            //string sql = "select OrderNo as 订单编号, OrderType as 订单状态,TotalQty as 订单数量 from View_OrderInfo where 1=1 ";
            ////生成查询参数
            //DynamicParameters dynamicParameters = new DynamicParameters();
            //wheres.ForEach(item =>
            //{
            //    if (item.Name == "OrderType" && !string.IsNullOrEmpty(item.Value))
            //    {
            //        sql += " and OrderType=@OrderType";
            //        dynamicParameters.Add("OrderType", item.Value);
            //    }
            //});

            ////执行查询
            //var rows = DBServerProvider.SqlDapper.QueryList<object>(sql, dynamicParameters)
            //              .Select(item => item as IDictionary<string, object>)
            //              .ToList();

            ////字典转换
            //var dicList = DictionaryManager.GetDictionary("订单状态").Sys_DictionaryList;
            //foreach (var item in rows)
            //{
            //    string value = item["订单状态"]?.ToString();
            //    item["订单状态"] = dicList.Where(x => x.DicValue == value).Select(x => x.DicName).FirstOrDefault();
            //}

            ////调用通过导出
            //string path = EPPlusHelper.ExportGeneralExcel(rows, "text.xlsx", onFillCell: (ExcelWorksheet worksheet, int rowIndex, int columnIndex, object value) =>
            //{
            //    //自定义excel内容或者格式，见epplus文档
            //});
            //return File(Sys.File.ReadAllBytes(path), MediaTypeNames.Application.Octet, "text.xlsx");
        }
    }
}
